package cn.okuribito.aop;

import cn.okuribito.model.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class ReponseBodyAop {

    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void request() {
    }

    @Around("request()")
    public Object around(ProceedingJoinPoint pjp) {
        try {
            Object proceed = pjp.proceed(pjp.getArgs());
            if (proceed instanceof Result) {
                return proceed;
            } else {
                return Result.SUCCESS(proceed);
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return Result.error(throwable.getMessage());
        }
    }

}
